﻿/*
	* Project	: SharePoint Dynamic Forms
	* Version	: 1.0
	* Company	: SharePoint Frontier Software Pvt Ltd. (http://www.sharepointfrontier.com)
	* Author	: Ashok Raja .T (Blog : http://www.ashokraja.me)
	* Licence	: Published under Microsoft Public License (Ms-PL)
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI;
using Microsoft.SharePoint.WebPartPages;

namespace SFS.SharePoint.DynamicForms
{
    public class PropertiesEditor : EditorPart
    {
        StringBuilder sb = new StringBuilder();
        private const string AscxPath = @"~/_CONTROLTEMPLATES/SFS.SharePoint.DynamicForms/DynamicFormEditorPart.ascx";
        private DynamicFormEditorPart ctl;

        protected override void CreateChildControls()
        {
            //ctl = (DynamicFormEditorPart)Page.LoadControl(AscxPath);
            //Controls.Add(ctl);
        }

        public override bool ApplyChanges()
        {
            if (IsValidInputData())
            {
                BaseDynamicWebpart wp = this.WebPartToEdit as BaseDynamicWebpart;
                wp.RenderingTemplate = ctl.ddlRenderType.SelectedItem.Text;
                wp.FormTitle = ctl.txtDisplayTitle.Text;
                wp.ListName = ctl.txtListName.Text;
                wp.TemplateSourceList = ctl.txtTemplateSource.Text;
                wp.TemplateID = ctl.txtTemplateId.Text;
                wp.TemplateIdColumn = ctl.txtTemplateIdColumn.Text;
                wp.TemplateDataColumn = ctl.txtTemplateData.Text;
                wp.SaveUrl = ctl.txtSaveurl.Text;
                wp.CancelUrl = ctl.txtCancelUrl.Text;
                wp.DisplayContentType = ctl.ckDisplayCtype.Checked;
                wp.IgnoreFields = ctl.txtIgnoreFields.Text;
                wp.ShowFields = ctl.txtShowFields.Text;
                wp.ShowCancel = ctl.ckShowCancel.Checked;
                wp.SaveCaption = ctl.txtSaveCaption.Text;
                wp.CancelCaption = ctl.txtCancelCaption.Text;
                wp.SaveButtonWidth = string.IsNullOrEmpty(ctl.txtSaveSize.Text) ? 0 : int.Parse(ctl.txtSaveSize.Text);
                wp.CancelButtonWidth = string.IsNullOrEmpty(ctl.txtCancelSize.Text) ? 0 : int.Parse(ctl.txtCancelSize.Text);
                wp.CloseFormOnSubmit = ctl.ckClose.Checked;
                wp.ShowFormHeader = ctl.ckShowHeader.Checked;
                wp.SaveChanges();

                return true;
            }
            else
                return false;
        }



        public override void SyncChanges()
        {
            if (ctl == null) return;
            BaseDynamicWebpart wp = this.WebPartToEdit as BaseDynamicWebpart;
            ctl.ddlRenderType.SelectedValue = wp.RenderingTemplate;
            ctl.txtDisplayTitle.Text = wp.FormTitle;
            ctl.txtListName.Text = wp.ListName;
            ctl.txtTemplateSource.Text = string.IsNullOrEmpty(wp.TemplateSourceList) ? "FormTemplates" : wp.TemplateSourceList;
            ctl.txtTemplateId.Text = wp.TemplateID;
            ctl.txtTemplateIdColumn.Text = string.IsNullOrEmpty(wp.TemplateIdColumn) ? "Title" : wp.TemplateIdColumn;
            ctl.txtTemplateData.Text = string.IsNullOrEmpty(wp.TemplateDataColumn) ? "FormData" : wp.TemplateDataColumn;
            ctl.txtSaveurl.Text = wp.SaveUrl;
            ctl.txtCancelUrl.Text = wp.CancelUrl;
            ctl.ckDisplayCtype.Checked = wp.DisplayContentType;
            ctl.txtIgnoreFields.Text = wp.IgnoreFields;
            ctl.txtShowFields.Text = wp.ShowFields;
            ctl.ckShowCancel.Checked = wp.ShowCancel;
            ctl.txtSaveCaption.Text = wp.SaveCaption;
            ctl.txtCancelCaption.Text = wp.CancelCaption;
            ctl.txtSaveSize.Text = wp.SaveButtonWidth == 0 ? string.Empty : wp.SaveButtonWidth.ToString();
            ctl.txtCancelSize.Text = wp.CancelButtonWidth == 0 ? string.Empty : wp.CancelButtonWidth.ToString();
            ctl.ckClose.Checked = wp.CloseFormOnSubmit;
            ctl.ckShowHeader.Checked = wp.ShowFormHeader;

        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            ctl = (DynamicFormEditorPart)Page.LoadControl(AscxPath);
            Controls.Add(ctl);
            this.Title = "SFS - Dynamic Forms";
        }

        private bool IsValidInputData()
        {
            sb.Length = 0;

            bool isValid = true;

            if (ctl.ddlRenderType.SelectedIndex == 0)
            {
                if (string.IsNullOrEmpty(ctl.txtListName.Text))
                {
                    sb.AppendLine("List Name is required.<br/>");
                    isValid = false;
                }
            }
            else
            {
                if (string.IsNullOrEmpty(ctl.txtTemplateSource.Text))
                {
                    sb.AppendLine("Template source is required.<br/>");
                    isValid = false;
                }

                if (string.IsNullOrEmpty(ctl.txtTemplateId.Text))
                {
                    sb.AppendLine("Template Id is required.<br/>");
                    isValid = false;
                }
            }

            if (!IsWidthValid(ctl.txtSaveSize.Text))
            {
                sb.AppendLine("Save button width must be a +ve integer.<br/>");
                isValid = false;
            }

            if (!IsWidthValid(ctl.txtCancelSize.Text))
            {
                sb.AppendLine("Cancel button width must be a +ve integer.<br/>");
                isValid = false;
            }
            if (sb.Length > 0)
            {
                ctl.ctlErr.Visible = true;
                ctl.lblError.Text = sb.ToString();
            }
            else
            {
                ctl.ctlErr.Visible = false;
                ctl.lblError.Text = "";
            }
            return isValid;
        }

        private bool IsWidthValid(string Data)
        {
            if (string.IsNullOrEmpty(Data))
                return true;
            uint ctlWidth = 0;
            return uint.TryParse(Data, out ctlWidth);
        }

        //protected override void OnPreRender(EventArgs e)
        //{
        //    if (!string.IsNullOrEmpty(sb.ToString()))
        //    {
        //        this.Zone.ErrorText = string.Format("{0}<br />{1}", this.Zone.ErrorText,
        //                               sb.ToString());
        //    }
        //    base.OnPreRender(e);
        //}
    }
}
